﻿//Copyright 2008 Kristian Gundry And Rune Juhl-Petersen
//This file is part of Plain.

//Plain is free software: you can redistribute it and/or modify
//it under the terms of the GNU Lesser General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//any later version.

//Plain is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public License
//along with Plain.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Web.UI;
using Plain.Domain;
using System.Reflection;


namespace Plain.Web
{
    public class PlainHierarchyDataSource : HierarchicalDataSourceControl
    {
        protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath)
        {
            PlainHierarchicalDataSourceView view = new PlainHierarchicalDataSourceView();
            view.Type = System.Type.GetType(type);
            view.SubNotes = subNotes;
            return view;
            //throw new NotImplementedException();
        }
        private string type;
        public string Type
        {
            get { return type; }
            set { type = value; }
        }
        private string subNotes;
        public string SubNotes {
            get { return subNotes; }
            set { subNotes = value; }
        }
    }
    public class PlainHierarchicalDataSourceView : HierarchicalDataSourceView{
        private Type type;
        private string subNotes;

        public virtual string SubNotes {
            get { return subNotes; }
            set { subNotes = value; }
        }
        public Type Type
        {
            get { return type; }
            set { type = value; }
        }

        public override IHierarchicalEnumerable Select(){
            Type dft = PlainApplication.Config.DaoFactory.GetType();
            MethodInfo[] methods = dft.GetMethods();
            MethodInfo g = null;
            object value = null;

            foreach (MethodInfo m in methods)
            {
                if (m.Name == "GetDao" && m.IsGenericMethod)
                {
                    g = m.MakeGenericMethod(type, type.GetProperty("Id").PropertyType);
                    break;
                }
            } object Dao = null;
            if (g != null)
                Dao = g.Invoke(PlainApplication.Config.DaoFactory, null);

            Type dt = Dao.GetType();
            MethodInfo dm = dt.GetMethod("Get");
            value = dm.Invoke(Dao, new object[] { 1 });
            //MethodInfo dm = dt.GetMethod("Search");

            //FileStream stream = new FileStream(path, FileMode.Open);
            //XamlReader reader = new XamlReader();
            //Criteria crit = (Criteria)XamlReader.Load(stream);


            //value = dm.Invoke(Dao, new object[] { crit });
            //return (System.Collections.IEnumerable)value;
            System.Collections.IList list = new ArrayList();
            list.Add(value);
            return new PlainHierarchyEnumerable(list,subNotes);
        }
    }

    public class PlainHierarchyEnumerable : IHierarchicalEnumerable
    {
        string subNotes;

        public string SubNotes {
            get { return subNotes; }
            set { subNotes = value; }
        }
        private IEnumerable list;
        public PlainHierarchyEnumerable(IEnumerable l,string subNotes) {
            list = l;
            this.subNotes = subNotes;
        }
        public PlainHierarchyEnumerable() { }
        #region IHierarchicalEnumerable Members

        public IHierarchyData GetHierarchyData(object enumeratedItem){
            return new PlainData(enumeratedItem,subNotes);
            //return null;
            //throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        public System.Collections.IEnumerator GetEnumerator()
        {
            if (list != null) {
                return list.GetEnumerator();
            }

            else
            {
                List<string> l = new List<string>();
                l.Add("asdfasd");
                return l.GetEnumerator();
            }
        }

        #endregion
    }
    public class PlainData : IHierarchyData
    {
        string subNotes;

        public string SubNotes {
            get { return subNotes; }
            set { subNotes = value; }
        }
        object data = null;
        string d;
        public PlainData(object d,string subNotes) {
            data = d;
            this.subNotes = subNotes;
        }
        public PlainData(string d) {
            this.d = d;
            data = d;
        }
        #region IHierarchyData Members

        public IHierarchicalEnumerable GetChildren()
        {
            if (!string.IsNullOrEmpty(subNotes)) {
                try {
                    object o = data.GetType().GetProperty(subNotes).GetValue(data, null);

                    return new PlainHierarchyEnumerable((IEnumerable)o, subNotes);
                } catch { }
            }
          
            return null;// 
            
        }

        public IHierarchyData GetParent()
        {
            throw new NotImplementedException();
        }

        public bool HasChildren{
            get{
                try {
                    ICollection o = data.GetType().GetProperty(subNotes).GetValue(data, null) as ICollection;
                    if (o != null) {
                        return o.Count > 0;
                    } else {
                        return false;
                    }
                } catch {
                    return false;
                }
            }
        }

        public object Item
        {
            get {
                return data;    
                
            
            }
        }

        public string Path
        {
            get {
                return "blag";
                //throw new NotImplementedException(); 
            }
        }

        public string Type
        {
            get { return data.GetType().ToString(); }
        }

        #endregion
    }


}
